Removing Newlines and Spaces

Hey everyone,
I was just wondering if anyone knew the command to remove extra newlines and spaces from a string? I know I read it somewhere in the DXL Reference manual, but I can't seem to find it anymore. If anyone could help that would be great. Thanks!
wcheng - Tue Dec 22 13:36:17 EST 2009

Re: Removing Newlines and Spaces
SystemAdmin - Mon Dec 28 08:43:38 EST 2009

//-----------------------------------------------------------------------------
// Trim -- delete heading and trailing spaces in passed string.
//-----------------------------------------------------------------------------
string Trim(string src) {
  int first = 0                         // First byte of given string
  int last = length(src)-1              // Last byte index
  while (last > 0 && isspace(src[last])) last--         // Skip trailing spaces
  while (isspace(src[first]) && first<last) first++     // Skip heading spaces
  if (src[first:last]==" ") return ""   // Only single blank left? - return ""
  return src[first:last]                // *** EXIT ***
  }                                     // --- Trim ---
//-----------------------------------------------------------------------------

 


You can find this function in lib\dxl\example\dxllim.dxl

 

Re: Removing Newlines and Spaces
Mathias Mamsch - Tue Dec 29 15:22:38 EST 2009

SystemAdmin - Mon Dec 28 08:43:38 EST 2009

//-----------------------------------------------------------------------------
// Trim -- delete heading and trailing spaces in passed string.
//-----------------------------------------------------------------------------
string Trim(string src) {
  int first = 0                         // First byte of given string
  int last = length(src)-1              // Last byte index
  while (last > 0 && isspace(src[last])) last--         // Skip trailing spaces
  while (isspace(src[first]) && first<last) first++     // Skip heading spaces
  if (src[first:last]==" ") return ""   // Only single blank left? - return ""
  return src[first:last]                // *** EXIT ***
  }                                     // --- Trim ---
//-----------------------------------------------------------------------------

 


You can find this function in lib\dxl\example\dxllim.dxl

 

Don't trust the examples :-) That function does not work as expected if the string contains only blanks and has a newline in there. Example: For the string "\n \t" the result will be "\n" instead of ""

Take this corrected one instead:
 

string trimWhitespace(string s) {
   int first = 0
   int last = ( length s ) - 1
   while ( last > 0 && isspace( s[ last ] ) ) last--
   while ( isspace( s[ first ] ) && first < last ) first++
   if ( first == last && isspace s[first] ) return ""
   return s[ first:last ]
}

Re: Removing Newlines and Spaces
Bosch - Tue Jul 12 07:08:59 EDT 2011

Mathias Mamsch - Tue Dec 29 15:22:38 EST 2009

Don't trust the examples :-) That function does not work as expected if the string contains only blanks and has a newline in there. Example: For the string "\n \t" the result will be "\n" instead of ""

Take this corrected one instead:
 

string trimWhitespace(string s) {
   int first = 0
   int last = ( length s ) - 1
   while ( last > 0 && isspace( s[ last ] ) ) last--
   while ( isspace( s[ first ] ) && first < last ) first++
   if ( first == last && isspace s[first] ) return ""
   return s[ first:last ]
}

hi,

is it possible to replace the spaces with symbols like "-" or "_"
i have one string like Hello test, so i want to replace that by "Hello-test" or "Hello_test".
Kindly let me know if its possible.

Thank you!

Regards,
Shruthi

Re: Removing Newlines and Spaces
OurGuest - Tue Jul 12 07:45:54 EDT 2011

Bosch - Tue Jul 12 07:08:59 EDT 2011
hi,

is it possible to replace the spaces with symbols like "-" or "_"
i have one string like Hello test, so i want to replace that by "Hello-test" or "Hello_test".
Kindly let me know if its possible.

Thank you!

Regards,
Shruthi

For short strings here is a simple approach:
 

Buffer b1=create
string ChangeSpace(string sInput, sCharacter)
{ b1=""
  int j, l=length sInput
  for(j=0;j<l;j++)
  { if(sInput[j]==' ') b1+=sCharacter
      else b1+=sInput[j]""
  } 
  return b1""
}
print ChangeSpace("Hello test", "-")
 
print "\n" ChangeSpace("Hello test", "_")

Re: Removing Newlines and Spaces
Bosch - Wed Jul 13 00:55:36 EDT 2011

OurGuest - Tue Jul 12 07:45:54 EDT 2011

For short strings here is a simple approach:
 

Buffer b1=create
string ChangeSpace(string sInput, sCharacter)
{ b1=""
  int j, l=length sInput
  for(j=0;j<l;j++)
  { if(sInput[j]==' ') b1+=sCharacter
      else b1+=sInput[j]""
  } 
  return b1""
}
print ChangeSpace("Hello test", "-")
 
print "\n" ChangeSpace("Hello test", "_")

Thank you so much.

Regards,
Shruthi

Re: Removing Newlines and Spaces
llandale - Wed Jul 13 12:06:02 EDT 2011

OurGuest - Tue Jul 12 07:45:54 EDT 2011

For short strings here is a simple approach:
 

Buffer b1=create
string ChangeSpace(string sInput, sCharacter)
{ b1=""
  int j, l=length sInput
  for(j=0;j<l;j++)
  { if(sInput[j]==' ') b1+=sCharacter
      else b1+=sInput[j]""
  } 
  return b1""
}
print ChangeSpace("Hello test", "-")
 
print "\n" ChangeSpace("Hello test", "_")

Nit-Picks:
o Good thing creating the buffer outside the function, makes it run quite a bit faster which matters when doing 1000s of replaces. I re-use the SAME global buffer for lots of my simple functions; so long as those functions don't call any other functions.
o At line 7 you don't need to convert the character to a string: "else b1+=sInput[j]" will do.
o I think you use less string space if you "return(stringOf(b1))", rather than converting it to a string constant and then returning it.
o I note with great curiosity you putting the previous line's EOL at the start of the NEXT line's print statement. That actually makes a lot of sense since you are much more likely to know that a printed string is supposed to be first in the line than last in the line. I'm trying to work myself up to doing that, but old habits die hard.

This is clumsy:
...print "Ids:\t"
...for objects do{ print ID "\t"}
...print "\n"
...
...print "This is the next line\n"
Note the extra tab at the end of the line.

This is graceful:
...print "\nIds:"
...for objects do{ print "\t" ID}
...
...print "\nThis is the next line"
Note that we know that each ID is preceded with a TAB, and the "Ids" and the "This is" lines always start a line.

This reminds of of the superior Hewlet-Packard "Reverse Polish Notation" calculators where you entered the value then decided what to do with it (add or subtract). Or the superior non-QUERTY type writers where common letter combinations dictate where the letters are. These are better, but folks cannot get used to them.

Re: Removing Newlines and Spaces
SystemAdmin - Thu Jul 21 09:09:57 EDT 2011

SystemAdmin - Mon Dec 28 08:43:38 EST 2009

//-----------------------------------------------------------------------------
// Trim -- delete heading and trailing spaces in passed string.
//-----------------------------------------------------------------------------
string Trim(string src) {
  int first = 0                         // First byte of given string
  int last = length(src)-1              // Last byte index
  while (last > 0 && isspace(src[last])) last--         // Skip trailing spaces
  while (isspace(src[first]) && first<last) first++     // Skip heading spaces
  if (src[first:last]==" ") return ""   // Only single blank left? - return ""
  return src[first:last]                // *** EXIT ***
  }                                     // --- Trim ---
//-----------------------------------------------------------------------------

 


You can find this function in lib\dxl\example\dxllim.dxl

 

Hi Pekka,
VERY NICE - not shouting, just very excited. :)
This is the kind of thing I am talking about! Nice explanations.
I know what it is doing, but still, very nice for the newbie that comes along and can say "YEA!".
We need more postings like this! :)
Thank you!
Jerry

Re: Removing Newlines and Spaces
gankir - Mon Sep 19 07:19:51 EDT 2011

Mathias Mamsch - Tue Dec 29 15:22:38 EST 2009

Don't trust the examples :-) That function does not work as expected if the string contains only blanks and has a newline in there. Example: For the string "\n \t" the result will be "\n" instead of ""

Take this corrected one instead:
 

string trimWhitespace(string s) {
   int first = 0
   int last = ( length s ) - 1
   while ( last > 0 && isspace( s[ last ] ) ) last--
   while ( isspace( s[ first ] ) && first < last ) first++
   if ( first == last && isspace s[first] ) return ""
   return s[ first:last ]
}

Hi,

Is there an adapatation to this code, which will remove the white spaces in between the strings. I understand this code removes the starting and trailing white spaces.

Thanks Up-front!!

Re: Removing Newlines and Spaces
llandale - Mon Sep 19 10:32:25 EDT 2011

gankir - Mon Sep 19 07:19:51 EDT 2011
Hi,

Is there an adapatation to this code, which will remove the white spaces in between the strings. I understand this code removes the starting and trailing white spaces.

Thanks Up-front!!

//**********************
bool    fIsEmptyBuf(Buffer in_buf)
{     // Is the input buffer Empty or null.
        // That is, will retrieving the string value from the buffer be null?
        // Note that retrieving the value to see if its null would be wasteful,
        //      and checking the 'length' is misleading, in case the user
        //      has inserted a null character inside the buffer.
        return(null in_buf              or 
                 length(in_buf) == 0    or
                 null in_buf[0])
}     // end fIsEmptyBuf()
 
 
 
 
Buffer  gl_fReplace_ResultsBuff = create(),
        gl_fReplace_ToStngBuff  = create()      // Houses ToString, allows quick Combine
 
 
 
//***************************
void    fReplaceCharacters(Buffer &in_buf, string in_Characters, in_ReplaceString)
{     // Replace each of the in_Characters found in the input buffer, with the Replace String.
        if (null in_buf         or 
            fIsEmptyBuf(in_buf) or 
            null in_Characters) return  // Nothing to do
 
        int     i, j
        char    c
        bool    Found
 
        gl_fReplace_ToStngBuff  = in_ReplaceString      // Combining Buffers is more efficient than appending Strings.
        gl_fReplace_ResultsBuff = ""
 
        for (i=0; i<length(in_buf); i++)
        {  c  = in_buf[i]
           Found        = false
           for (j=0; j<length(in_Characters); j++)
           {  if (c == in_Characters[j])
              {  Found        = true
                 break
              }
           }  // end for each in_Characters
           if (Found)   //-
           then combine(gl_fReplace_ResultsBuff, gl_fReplace_ToStngBuff, 0)     // Replace the character
           else gl_fReplace_ResultsBuff += c                                            // Preserve the Character
        }     // end for every character in in_buf.
 
        in_buf  = ""                  // Replace the results
        combine(in_buf, gl_fReplace_ResultsBuff, 0)
 
}    // end fReplaceCharacters(Buffer)

 


The following global constant is helpful, declaring which characters are considered 'white':

const string cl_White = " \t\n\v\f\r" charOf(160) "" // White Space. see above comments.
#160 is MS-Word non-breaking space.

Just now realized that will replace individual characters, e.g. replace space-tab-space with ",,,". I think I need a new function that will replace sequences of characters with a single replaceString; e.g. replace space-tab-space with ",". Maybe someone can write "fReplaceCharSequences()", and test it converting all sequences of white space with a single space.

 

 

 

  • Louie

 

 

Re: Removing Newlines and Spaces
Doug_Wilson - Fri Apr 17 12:20:29 EDT 2015

SystemAdmin - Mon Dec 28 08:43:38 EST 2009

//-----------------------------------------------------------------------------
// Trim -- delete heading and trailing spaces in passed string.
//-----------------------------------------------------------------------------
string Trim(string src) {
  int first = 0                         // First byte of given string
  int last = length(src)-1              // Last byte index
  while (last > 0 && isspace(src[last])) last--         // Skip trailing spaces
  while (isspace(src[first]) && first<last) first++     // Skip heading spaces
  if (src[first:last]==" ") return ""   // Only single blank left? - return ""
  return src[first:last]                // *** EXIT ***
  }                                     // --- Trim ---
//-----------------------------------------------------------------------------

 


You can find this function in lib\dxl\example\dxllim.dxl

 

Hi,

This is in regard to the SystemAdmin post dated 12/28/2009. The only modification that I made to the code shown was removing the line of code that would trim at the beginning of the string as I didn't need that.    This "Trim" function worked reasonablly well.  But two problems that I've noticed with it.  The first one being that where an object contained an inserted OLE object, this function removed the OLE object leaving the Object Text attribute empty.  The second problem I saw with it is that it removed the bold setting  on some text in the original string.  So a string that originally started out as:

Design Note: The present ...

came out looking like this:

Design Note: The present...

Would anyone know a simple way to keep these two things from happening?

Thanks, Doug

Re: Removing Newlines and Spaces
Wolfgang Uhr - Mon Apr 20 05:06:14 EDT 2015

Doug_Wilson - Fri Apr 17 12:20:29 EDT 2015

Hi,

This is in regard to the SystemAdmin post dated 12/28/2009. The only modification that I made to the code shown was removing the line of code that would trim at the beginning of the string as I didn't need that.    This "Trim" function worked reasonablly well.  But two problems that I've noticed with it.  The first one being that where an object contained an inserted OLE object, this function removed the OLE object leaving the Object Text attribute empty.  The second problem I saw with it is that it removed the bold setting  on some text in the original string.  So a string that originally started out as:

Design Note: The present ...

came out looking like this:

Design Note: The present...

Would anyone know a simple way to keep these two things from happening?

Thanks, Doug

Hi

There is a way but  is is not simple.

string sNormal = objWork."myAttribute";
string sRichText = richText(objWork."myAttribute");

There are two possibilities to read an attribute value. The second delivers the rich text string and this string contains all information of the text style and the oles inside.

But the modification of this string is not as simple as you need. Perhaps this library may help you: http://nrtftree.sourceforge.net/jrtftree.html

Best regards

Wolfgang